package com.heiye.blog.ai.event.listener;

import com.heiye.blog.ai.domain.mapper.ChatMessageMapper;
import com.heiye.blog.ai.event.RoleServiceSoundGenerationEvent;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author: heiye
 * @date: 2025/11/21 下午8:40
 * @version: v1.0.0
 * @description: 角色服务声音生成监听器
 */
@Slf4j
@Component
public class RoleServiceSoundGenerationListener {

    @Resource
    private ChatMessageMapper chatMessageMapper;

    /**
     * 音频地址更新数据库
     *
     * @param event
     */
    @EventListener
    // 指定使用我们自定义的线程池
    @Async("eventTaskExecutor")
    public void audioUrlUpdateDB(RoleServiceSoundGenerationEvent event) {
        log.info("## roleServiceSoundGenerationEvent: {}", event);

        // 获取消息 ID
        Long id = event.getId();
        // 获取音频地址
        String audioUrl = event.getAudioUrl();

        // 更新数据库
        chatMessageMapper.updateAudioUrlById(id, audioUrl);
    }
}
